package turtle.system.pt;


/**
 * Maximum DrawDown (see page 127).
 * @author ktchuang
 *
 */
public class MDD {
	double value = 0;
	boolean leastIsNegative = true;
	int maxNegCount = 0;
	int maxPosiCount = 0;
	int conNegCount = 0;	
	int conPosiCount = 0;
	int negCount = 0;
	int posiCount = 0;
	double max = 0;
	double min = 0;
	public MDD() {
      
    }
    public void insertData(double profit) {
    	negCount+= profit<0 ? 1:0;
    	posiCount+= profit>0 ? 1:0; 
    	value += profit;
    	if (max >=0 && value>max)
    		max = value;
    	if (min <=0 && value<min)
    		min = value;
    	
    	if (profit<0 && leastIsNegative) {
    		conNegCount ++;
    	}    	
    	else if (profit>0 && leastIsNegative) {
    	    maxNegCount = conNegCount>maxNegCount? 	conNegCount:maxNegCount;
    	    conNegCount = 0;
    	    conPosiCount ++;
    	    leastIsNegative = false;
    	}
    	else if (profit>0 && !leastIsNegative) {
    		conPosiCount ++;
    	}
    	else if (profit<0 && !leastIsNegative) {
    		maxPosiCount = conPosiCount>maxPosiCount? 	conPosiCount:maxPosiCount;
    		conPosiCount = 0;
    		conNegCount ++;
    		leastIsNegative = true;
    	}    	
    }
    public int getNegativeCount() {
    	return this.negCount;
    }
    public int getPositiveCount() {
    	return this.posiCount;
    }
    public double getMaxHighLowDiff() {
    	return 0;
    }
    public double getMaxDrawDown() {
    	return min;
    }
    public double getMaxProfit() {
    	return max;
    }
    public String toString() {
    	StringBuffer buf = new StringBuffer();
    	buf.append("期間最大震幅:"+this.getMaxHighLowDiff()+"\n");
    	buf.append("期間最大正值:"+this.getMaxProfit()+"\n");
    	buf.append("期間最大負值:"+this.getMaxDrawDown()+"\n");
    	buf.append("期間損失次數:"+this.getNegativeCount()+"\n");
    	buf.append("期間獲利次數:"+this.getPositiveCount()+"\n");
    	buf.append("期間最多連續正值:"+this.maxPosiCount+"\n");
    	buf.append("期間最大連續負值:"+this.maxNegCount+"\n");
    	buf.append("最終獲利值:"+this.value+"\n");
    	if (this.value<=0) {
    	  buf.append("MAR:-"+Math.abs(this.value/this.getMaxDrawDown())+"\n");
    	}
    	else {
    		buf.append("MAR:"+Math.abs(this.value/this.getMaxDrawDown())+"\n");
    	}
    	return buf.toString();
    }
}
